package com.mini.controller;

import com.mini.conditon.BusinessChanceMaxSetCondition;
import com.mini.config.BusinessException;
import com.mini.config.CheckLogin;
import com.mini.model.BusinessChanceMaxSet;
import com.mini.model.BusinessTeacherInfo;
import com.mini.service.BusinessChanceMaxSetService;
import com.mini.service.BusinessTeacherInfoService;
import com.mini.vo.BusinessResult;
import com.mini.vo.CommonResultMessage;
import com.mini.vo.PageResultVO;
import com.mini.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: xiyuefei
 * Date: 2020/12/15
 * Time: 下午6:07
 */
@Slf4j
@RestController
@RequestMapping(value = "/businessChanceMaxSet")
@Api(tags = "分配上限设置服务相关接口")
public class BusinessChanceMaxSetController extends BaseController{
    @Autowired
    private BusinessChanceMaxSetService businessChanceMaxSetService;

    @Autowired
    private BusinessTeacherInfoService businessTeacherInfoService;


    @CheckLogin
    @Deprecated
    @PostMapping(value = "/pageList")
    @ApiOperation(value = "分配上限查询" , notes = "分配上限查询")
    public BusinessResult<PageResultVO<BusinessChanceMaxSet>> groupBasePageList1(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                                @RequestBody BusinessChanceMaxSetCondition condition){
        List<UserVO> userInfos = businessChanceMaxSetService.getUserListByCondition(condition,Authorization);

        List<Long> userIdList = new ArrayList<>();
        for(UserVO userVO : userInfos){
            userIdList.add(userVO.getId());
        }
        Map<String,UserVO> resultMap = new HashMap();
        for (UserVO userVO : userInfos){
            resultMap.put(userVO.getId()+"",userVO);
        }

        condition.setUserIdList(userIdList);
        List<BusinessTeacherInfo> result = businessTeacherInfoService.page1(condition);
        List<BusinessChanceMaxSet> resultEnd = new ArrayList<>();
        for(BusinessTeacherInfo userVO : result){
            BusinessChanceMaxSet temp = new BusinessChanceMaxSet();
            temp.setUserId(userVO.getTeacherId());
            UserVO usertemp = resultMap.get(userVO.getTeacherId()+"");
            temp.setNickname(usertemp == null ? "":usertemp.getNickname());
            temp.setUsername(usertemp == null ? "":usertemp.getUsername());
            temp.setChanceEveryDay(userVO.getAddWechatNum());
            resultEnd.add(temp);
        }



        return BusinessResult.createSuccessInstance(resultEnd);
    }

    @CheckLogin
    @PostMapping(value = "/pageList1")
    @ApiOperation(value = "分配上限查询" , notes = "分配上限查询")
    public BusinessResult<PageResultVO<BusinessChanceMaxSet>> groupBasePageList(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                                @RequestBody BusinessChanceMaxSetCondition condition){
        List<UserVO> userInfos = businessChanceMaxSetService.getUserListByCondition(condition,Authorization);

        List<Long> userIdList = new ArrayList<>();
        for(UserVO userVO : userInfos){
            userIdList.add(userVO.getId());
        }

        condition.setUserIdList(userIdList);
        List<BusinessChanceMaxSet> result = businessChanceMaxSetService.businessChanceMaxSetList(condition);
        Map<String,BusinessChanceMaxSet> resultMap = new HashMap();
        for (BusinessChanceMaxSet businessChanceMaxSet : result){
            resultMap.put(businessChanceMaxSet.getUserId()+"",businessChanceMaxSet);
        }
        List<BusinessChanceMaxSet> resultEnd = new ArrayList<>();
        for(UserVO userVO : userInfos){
            BusinessChanceMaxSet temp = new BusinessChanceMaxSet();
            temp.setUserId(userVO.getId());
            temp.setNickname(userVO.getNickname());
            temp.setUsername(userVO.getUsername());
            temp.setChanceEveryDay(0);
            if(resultMap.containsKey(userVO.getId()+"")){
                BusinessChanceMaxSet t = resultMap.get(userVO.getId()+"");
                temp.setUpdateTime(t.getUpdateTime());
                temp.setCreateTime(t.getCreateTime());
                temp.setId(t.getId());
                temp.setChanceEveryDay(t.getChanceEveryDay());
                temp.setChanceUnfollow(t.getChanceUnfollow());
                temp.setEditUserId(t.getEditUserId());
            }
            resultEnd.add(temp);
        }


        return BusinessResult.createSuccessInstance(resultEnd);
    }



    @CheckLogin
    @PostMapping(value = "/save")
    @ApiOperation(value = "保存分配上限" , notes = "保存分配上限")
    public BusinessResult businessChanceMaxSetSave(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                            @RequestBody BusinessChanceMaxSet businessChanceMaxSet){
        if (Objects.isNull(businessChanceMaxSet)) {
            throw new BusinessException("参数不合法");
        }
        if (businessChanceMaxSet.getEditUserId() == null) {
            throw new BusinessException("操作人为空");
        }
        if (businessChanceMaxSet.getUserId() == null) {
            throw new BusinessException("运营人为空");
        }
        if (businessChanceMaxSet.getChanceEveryDay() == null) {
            throw new BusinessException("每日分配的新机会的上限值为空");
        }

        businessChanceMaxSetService.saveBusinessChanceMaxSet(businessChanceMaxSet);

        return BusinessResult.createInstance(CommonResultMessage.SUCCESS);
    }
}
